home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / Out of This GWorld / draw.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.0 KB  |  304 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        draw.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: EL    
  7.  
  8.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 08/2000        JM                Carbonized, non-Carbon code is commented out
  20.                                             for demonstration purposes.
  21.                 7/16/1999    KG                Updated for Metrowerks Codewarror Pro 2.1
  22.                 
  23.  
  24. */
  25. #include "out.h"
  26.  
  27. void shadeSCurve(int x, int y, int radout, int radin, int index);
  28. void drawColorSpheres(void);
  29.  
  30. void createColorScale()
  31. {
  32.     int i;
  33.     int x;
  34.     int down = true;
  35.     int    index = 8;
  36.  
  37.     PenSize( 14, 1 );
  38.     
  39.     for (x = -420; x < 420; x += 105)
  40.     {
  41.         for (i = 0; i < 30; i++)
  42.         {
  43.             setColor( index );
  44.             
  45.             if (down == true)
  46.                 drawLine( x, 420 - (i * 28) , x, 420 - ((i + 1) * 28) );
  47.             else
  48.                 drawLine( x, -420 + (i * 28) , x, -420 + ((i + 1) * 28) );
  49.             index++;
  50.         }
  51.         
  52.         down = 1 - down;
  53.     }
  54.     
  55.     down = false;
  56.     index = 8;
  57.  
  58.     for (x = 368; x > -472; x -= 105)
  59.     {
  60.         for (i = 0; i < 30; i++)
  61.         {
  62.             setColor( index );
  63.             
  64.             if (down == true)
  65.                 drawLine( x, 420 - (i * 28) , x, 420 - ((i + 1) * 28) );
  66.             else
  67.                 drawLine( x, -420 + (i * 28) , x, -420 + ((i + 1) * 28) );
  68.             index++;
  69.         }
  70.         
  71.         down = 1 - down;
  72.     }
  73. }
  74.  
  75. void createColorWheels()
  76. {
  77.     testTriangle( -400, 300 );
  78.     testTriangle( 400, 300 );
  79.     testTriangle( 400, -300 );
  80.     testTriangle( -400, -300 );
  81.     shadeWasher( 0, 0, 360, 80 );
  82. }
  83.  
  84. void createColorRings()
  85. {
  86.     shadeWasher( 0, 0, 500, 400 );
  87.     shadeWasher( 0, 0, 400, 300 );
  88.     shadeWasher( 0, 0, 300, 200 );
  89.     shadeWasher( 0, 0, 200, 100 );
  90. }
  91.  
  92. void createColorGears()
  93. {
  94.     shadeWasher( 150, 150, 300, 100 );
  95.     shadeWasher( -150, 150, 300, 100 );
  96.     shadeWasher( -150, -150, 300, 100 );
  97.     shadeWasher( 150, -150, 300, 100 );
  98. }
  99.  
  100. void createColorCurves()
  101. {
  102.     shadeWasher( 720, 720, 900, 0 );
  103.     shadeWasher( 720, -720, 900, 0 );
  104.     shadeWasher( -720, -720, 900, 0 );
  105.     shadeWasher( -720, 720, 900, 0 );
  106.     shadeWasher( 0, 0, 400, 0 );
  107. }
  108.  
  109. void createColorBalls()
  110. {
  111.     int x, y;
  112.  
  113.     for (x = -432; x <= 432; x += 108)
  114.         for (y = -432; y <= 432; y += 108)
  115.             shadeWasher( x, y, 60, 0 );
  116. }
  117.  
  118. void createColorWave()
  119. {
  120.     shadeSCurve( -300, 250, 210, 60, 9 );
  121.     shadeSCurve( 96, 250, 210, 60, 69 );
  122.     shadeSCurve( -300, 150, 210, 60, 129 );
  123.     shadeSCurve( 96, 150, 210, 60, 189 );
  124.     shadeSCurve( -300, 50, 210, 60, 9 );
  125.     shadeSCurve( 96, 50, 210, 60, 69 );
  126.     shadeSCurve( -300, -50, 210, 60, 129 );
  127.     shadeSCurve( 96, -50, 210, 60, 189 );
  128.     shadeSCurve( -300, -150, 210, 60, 9 );
  129.     shadeSCurve( 96, -150, 210, 60, 69 );
  130.     shadeSCurve( -300, -250, 210, 60, 129 );
  131.     shadeSCurve( 96, -250, 210, 60, 189 );
  132. }
  133.  
  134. void createColorText()
  135. {
  136.     Rect        rect;
  137.     PicHandle    thePict;
  138.     int            width, height;
  139.     
  140.     thePict = (PicHandle)GetResource( 'PICT', 129 );
  141.     
  142.     width = (**thePict).picFrame.right - (**thePict).picFrame.left;
  143.     height = (**thePict).picFrame.bottom - (**thePict).picFrame.top;
  144.  
  145.     SetRect( &rect, (WWIDTH - width) / 2, (WHEIGHT - height) / 2, 
  146.                     ((WWIDTH - width) / 2) + width, ((WHEIGHT - height) / 2 ) + height );
  147.  
  148.     DrawPicture( thePict, &rect );
  149.     ReleaseResource( (Handle)thePict );
  150. }
  151.  
  152. void drawColorSpheres()
  153. {
  154.     int rad;
  155.     int i;
  156.  
  157.     for (i = 9; i < 48; i++)
  158.     {
  159.         setColor( i );
  160.         rad = (48 - i) * 3;
  161.         drawCircle( (int)(i * 1.414), (int)(i * 1.414), rad );
  162.     }
  163. }
  164.  
  165. void testTriangle( x, y )
  166. int x, y;
  167. {
  168.     PenSize( 1, 1 );
  169.  
  170.     setColor( 7 );
  171.     drawCircle( x, y, 25 );
  172.  
  173.     setColor( 1 );
  174.     drawWasher( x, y, 25, 60, -30, 30 );
  175.     drawLine( x - 60, y + 40, x, y + 140 );
  176.     drawLine( x, y + 140, x + 60, y + 40 );
  177.     setColor( 2 );
  178.     drawWasher(x, y, 25, 60, 30, 90 );
  179.     setColor( 3 );
  180.     drawWasher(x, y, 25, 60, 90, 150 );
  181.     drawLine( x + 60, y + 40, x + 121, y - 70 );
  182.     drawLine( x + 121, y - 70, x, y - 70 );
  183.     setColor( 4 );
  184.     drawWasher( x, y, 25, 60, 150, 210 );
  185.     setColor( 5 );
  186.     drawWasher( x, y, 25, 60, 210, 270 );
  187.     drawLine( x - 60, y + 40, x - 121, y - 70 );
  188.     drawLine( x - 121, y - 70, x, y - 70 );
  189.     setColor( 6 );
  190.     drawWasher( x, y, 25, 60, 270, 330 );
  191. }
  192.  
  193. void shadeWasher( x, y, radout, radin )
  194. int x, y;
  195. int radout, radin;
  196. {
  197.     int ang;
  198.     int i = 8;
  199.     int radmid;
  200.  
  201.     radmid = radin + (radout - radin) / 2;
  202.     
  203.     for (ang = 0; ang < 360; ang += 3)
  204.     {
  205.         setColor( i );
  206.         drawWasher( x, y, radmid, radout, ang, ang + 3 );
  207.         i++;
  208.     }
  209.     
  210.     for (ang = 0; ang < 360; ang += 3)
  211.     {
  212.         setColor( i );
  213.         drawWasher( x, y, radin, radmid, ang, ang + 3 );
  214.         i++;
  215.     }
  216. }
  217.  
  218. void shadeSCurve( x, y, radout, radin, index)
  219. int x, y;
  220. int radout, radin;
  221. int index;
  222. {
  223. #pragma unused (radout)
  224. #pragma unused (radin)
  225.  
  226.     int ang;
  227.     
  228.     for (ang = 270; ang > 90; ang -= 6)
  229.     {
  230.         setColor( index );
  231.         drawWasher( x, y, 60, 210, ang, ang - 6 );
  232.         index++;
  233.     }
  234.  
  235.     for (ang = -90; ang < 90; ang += 6)
  236.     {
  237.         setColor( index );
  238.         drawWasher( x + 198, y, 60, 210, ang, ang + 6 );
  239.         index++;
  240.     }
  241. }
  242.  
  243. void translate( x, y )
  244. int *x, *y;
  245. {
  246.     int xx, yy;
  247.     
  248.     xx = *x / SCALE;
  249.     yy = *y / SCALE;
  250.     
  251.     *x = xx + (WWIDTH / 2);
  252.     *y = (-yy) + (WHEIGHT / 2);
  253. }
  254.  
  255. void scale( len )
  256. int *len;
  257. {
  258.     *len /= SCALE;
  259. }
  260.  
  261. void drawCircle( x, y, radius )
  262. int x, y;
  263. int radius;
  264. {
  265.     Rect     rect;
  266.     int        foo = 0;
  267.     
  268.     translate( &x, &y );
  269.     scale( &radius );
  270.  
  271.     SetRect( &rect, x - radius, y - radius, x + radius, y + radius );
  272.     PaintOval( &rect );
  273. }
  274.  
  275. void drawWasher( x, y, irad, orad, sang, eang )
  276. int x, y;
  277. int irad, orad;
  278. int sang, eang;
  279. {
  280.     int        width;
  281.     Rect    rect;
  282.     
  283.     translate( &x, &y );
  284.     scale( &irad );
  285.     scale( &orad );
  286.     
  287.     width = (orad - irad) / 2;
  288.     
  289.     PenSize( width, width );
  290.     SetRect( &rect, x - (irad + width), y - (irad + width),
  291.                     x + (irad + width), y + (irad + width) );
  292.                     
  293.     FrameArc( &rect, sang, eang - sang );
  294.     PenSize( 1, 1 );
  295. }
  296.  
  297. void drawLine( sx, sy, ex, ey )
  298. {    
  299.     translate( &sx, &sy );
  300.     translate( &ex, &ey );
  301.     
  302.     MoveTo( sx, sy );
  303.     LineTo( ex, ey );
  304. }